package com.itheima.tanhua.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.tanhua.api.db.UserInfoApi;
import com.itheima.tanhua.api.mongodb.MovementApi;
import com.itheima.tanhua.pojo.db.UserInfo;
import com.itheima.tanhua.pojo.mongo.Movement;
import com.itheima.tanhua.utils.RelativeDateFormat;
import com.itheima.tanhua.vo.MovementsVo;
import com.itheima.tanhua.vo.PageResult;
import com.itheima.tanhua.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class ManagerService {


    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private MovementApi movementApi;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //用户数据翻页  用户的数据并分页
    public PageResult findAll(Long uid, String nickname, Integer page, Integer pagesize, String city) {
        //查询所有的用户
        List<UserInfo> list = userInfoApi.findAll();

        List<UserInfoVo> vos = list.stream().map(userInfo -> {
            UserInfoVo userInfoVo = new UserInfoVo();
            BeanUtil.copyProperties(userInfo, userInfoVo);
            userInfoVo.setId(Convert.toInt(userInfo.getId()));
            String s = stringRedisTemplate.opsForValue().get("USERFREEZE_" + uid);
            if (StrUtil.isNotBlank(s)){
                String[] split = s.split(",");
                userInfoVo.setUserStatus(split[0]);
            }
            return userInfoVo;
        }).skip((page - 1) * pagesize).limit(pagesize).collect(Collectors.toList());
        return new PageResult(page, pagesize, vos, 10, 100);
    }

    //查询用户全部的详细信息
    public UserInfoVo findById(Long uid) {
        UserInfo userInfo = userInfoApi.selectById(uid);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtil.copyProperties(userInfo, userInfoVo);
        userInfoVo.setId(Convert.toInt(userInfo.getId()));
        String s = stringRedisTemplate.opsForValue().get("USERFREEZE_" + uid);
        if (StrUtil.isNotBlank(s)){
            String[] split = s.split(",");
            userInfoVo.setUserStatus(split[0]);
        }
        return userInfoVo;
    }

    public PageResult findAllMovements(Map<String, String> map) {

        Long uid = Convert.toLong(map.get("uid"));
        int page = Convert.toInt(map.get("page"));
        int pagesize = Convert.toInt(map.get("pagesize"));
        //获取当前用户的所有动态 审核通过的
        //获取的值是经过审核之后的
        List<Movement> movementAll = movementApi.findMovementAll(uid, page, pagesize);
        //通过用户uid获取用户的详细信息
        UserInfo userInfo = userInfoApi.selectById(uid);
        List<MovementsVo> list = new ArrayList<>();
        for (Movement movement : movementAll) {
            MovementsVo movementsVo = new MovementsVo();
            BeanUtil.copyProperties(userInfo, movementsVo, "id");
            BeanUtil.copyProperties(movement, movementsVo, "id");
            movementsVo.setImageContent(movement.getMedias().toArray(new String[0]));
            movementsVo.setId(movement.getId().toHexString());
            movementsVo.setCreateDate(RelativeDateFormat.format(new Date(movement.getCreated())));
            list.add(movementsVo);
        }
        return new PageResult(page, pagesize, list, 10, 0);
    }

    //用户冻结
    public Map userFreeze(Map params) {

        //获取冻结的时间
        Integer freezingTime = Convert.toInt(params.get("freezingTime"));
        //冻结用户
        Long userId = Convert.toLong(params.get("userId"));
        int days = 0;
        if (freezingTime == 1) {
            days = 3;
        }
        if (freezingTime == 2) {
            days = 7;
        }
        if (freezingTime == 3) {
            days = -1;//永久冻结
        }
        //将冻结的信息保存到json中
        String value = JSON.toJSONString(params);
        //1为正常,2为冻结  保存到redis中
        stringRedisTemplate.opsForValue().set("USERFREEZE_" + userId, value, days, TimeUnit.DAYS);
        //封装返回结果
        Map map = new HashMap();
        map.put("message", "冻结成功");
        return map;
    }
}
